package com.boxing.servlet;

import com.boxing.beans.BookId;
import com.boxing.beans.Car;
import com.boxing.beans.CarResult;
import com.boxing.beans.Result;
import com.boxing.service.CarService;
import com.boxing.utils.Utils;
import com.google.gson.JsonObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/car/*")
public class CarServlet extends BaseServlet{
    private CarService carService = new CarService();

    /**
     * 添加购物车
     */
    public void addBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String s = new String(req.getInputStream().readAllBytes(), "utf-8");
        BookId bookId = gson.fromJson(s, BookId.class);
        Car car = new Car();
        car.setBook_id(bookId.getId());
        car.setEmail(Utils.getUserEmailFromSession(req));
        Result<Object> result = carService.addBook(car);
        resp.getWriter().print(gson.toJson(result));
    }

    /**
     * 获取购物车中所有的商品
     */
    public void getCar(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Result<CarResult> carResult = carService.getCar(req);
        resp.getWriter().print(gson.toJson(carResult));
    }
}
